from fastmcp import FastMCP  
import asyncio  
from fastmcp import Client  
import akshare as ak  
  
# 必须先初始化MCP实例  
mcp = FastMCP('report.mcp')  
  
@mcp.tool()  
def get_report_summary() -> list:  
    """  
    获取财经研报摘要数据(静态fallback数据)  
      
    :result: 返回研报列表,每个元素包含标题、摘要、来源和日期信息  
    """  
    return [  
        {  
            "title": "新能源行业深度报告",  
            "summary": "随着碳中和政策推进，新能源行业将迎来快速发展期，建议关注光伏、风电龙头企业。",  
            "source": "某证券研究所",  
            "date": "2023-11-15"  
        },  
        {  
            "title": "半导体产业链分析",  
            "summary": "国产替代加速，半导体设备和材料国产化率提升，建议关注相关细分领域龙头。",  
            "source": "某投资银行",  
            "date": "2023-11-10"  
        }  
    ]  
  
@mcp.tool()  
def get_report_from_akshare() -> list:  
    """  
    使用AKShare获取东方财富研报数据(官方API,最稳定)  
      
    :result: 返回研报列表,每个元素包含标题、机构、日期等信息  
    """  
    try:  
        # 使用AKShare的东方财富研报接口  
        # 获取最新的研报数据  
        report_df = ak.stock_report_fund_hold()  
          
        reports = []  
        # 获取前10条研报  
        for _, row in report_df.head(10).iterrows():  
            reports.append({  
                "title": row.get('股票简称', '') + "研报",  
                "stock_code": row.get('股票代码', ''),  
                "stock_name": row.get('股票简称', ''),  
                "date": str(row.get('报告期', '')),  
                "source": "东方财富网"  
            })  
          
        return reports if reports else [{"提示": "暂无研报数据"}]  
    except Exception as e:  
        print(f"AKShare获取研报失败: {e}")  
        # 失败时返回静态数据  
        return get_report_summary()  
  
@mcp.tool()  
def get_report_em_yjyg() -> list:  
    """  
    使用AKShare获取东方财富业绩预告数据(备用接口)  
      
    :result: 返回业绩预告列表  
    """  
    try:  
        # 使用AKShare的业绩预告接口  
        yjyg_df = ak.stock_yjyg_em()  
          
        reports = []  
        for _, row in yjyg_df.head(10).iterrows():  
            reports.append({  
                "title": f"{row.get('股票简称', '')}业绩预告",  
                "stock_code": row.get('股票代码', ''),  
                "stock_name": row.get('股票简称', ''),  
                "summary": row.get('预告内容', ''),  
                "date": str(row.get('首次预告日期', '')),  
                "source": "东方财富网"  
            })  
          
        return reports if reports else [{"提示": "暂无业绩预告数据"}]  
    except Exception as e:  
        print(f"AKShare获取业绩预告失败: {e}")  
        return get_report_summary()  

 REPORT_SUMMARY_SCHEMA = {  
    "type": "function",  
    "function": {  
        "name": "get_report_summary",  
        "description": "获取财经研报摘要数据(静态fallback数据)",  
        "parameters": {  
            "type": "object",  
            "properties": {},  
            "required": []  
        }  
    }  
}  
  
REPORT_AKSHARE_SCHEMA = {  
    "type": "function",  
    "function": {  
        "name": "get_report_from_akshare",  
        "description": "使用AKShare获取东方财富研报数据(官方API,最稳定)",  
        "parameters": {  
            "type": "object",  
            "properties": {},  
            "required": []  
        }  
    }  
}  
  
REPORT_YJYG_SCHEMA = {  
    "type": "function",  
    "function": {  
        "name": "get_report_em_yjyg",  
        "description": "使用AKShare获取东方财富业绩预告数据(备用接口)",  
        "parameters": {  
            "type": "object",  
            "properties": {},  
            "required": []  
        }  
    }  
} 
  
async def main():  
    client = Client(mcp)  
    async with client:  
        # 查看可用工具  
        tools = await client.list_tools()  
        print('可用工具:', tools)  
        print(f'工具数量: {len(tools)}')  
          
        # 调用静态研报工具  
        result1 = await client.call_tool('get_report_summary', {})  
        print('\n=== 静态研报结果 ===')  
        print(result1)  
          
        # 调用AKShare研报工具  
        result2 = await client.call_tool('get_report_from_akshare', {})  
        print('\n=== AKShare研报结果 ===')  
        print(result2)  
          
        # 调用业绩预告工具  
        result3 = await client.call_tool('get_report_em_yjyg', {})  
        print('\n=== 业绩预告结果 ===')  
        print(result3)  
  
if __name__ == '__main__':  
    asyncio.run(main())